package cn.chinaflame.pagodas.pool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;

import cn.chinaflame.pagodas.entity.PaLightsLog;
import cn.chinaflame.pagodas.service.PaLightsLogService;
import cn.chinaflame.pagodas.service.WrmNIService;
import cn.chinaflame.pagodas.spring.ApplicationContextHolder;
import cn.chinaflame.pagodas.utils.LightVO2LightLog;
import cn.chinaflame.pagodas.utils.LightAlgo.LightOp;
import cn.chinaflame.pagodas.vo.LightVO;


@Component
public class LightTask implements Callable<List<LightVO>>{
	
	private static final Logger logger = LoggerFactory.getLogger(LightTask.class);

	@Autowired
	private WrmNIService wrmNIService;
	
//	@Autowired
//	private PaLightsLogService paLightsLogService;//点灯、清灯日志Service
	
	private ArrayList<String> lightnos;//灯位编号
	private LightOp op;//操作类型，寻灯，安灯，清灯，检查灯位中的一种
	private JSONArray lightvos;//安灯参数jsonarray
	public LightTask() {}
	public LightTask(JSONArray lightvos,LightOp op) {
		this.lightvos = lightvos;
		this.op = op;
	}
	public LightTask(ArrayList<String> lightnos,LightOp op) {
		this.lightnos = lightnos;
		this.op = op;
	}
	public ArrayList<String> getLightnos() {
		return lightnos;
	}
	public void setLightnos(ArrayList<String> lightnos) {
		this.lightnos = lightnos;
	}
	public LightOp getOp() {
		return op;
	}
	public void setOp(LightOp op) {
		this.op = op;
	}
	
	@Override
	public List<LightVO> call() throws Exception {
		List<LightVO> vos = Lists.newArrayList();
		if(wrmNIService == null)
			 wrmNIService = ApplicationContextHolder.getBean(WrmNIService.class);//若调用WebService的service为空。则生成一个并打开开关
		wrmNIService.lighton();
		String lights;
		switch(op) {
		case lightCheck://执行检查灯位线程并打印信息
			lights = String.join(",", lightnos);
			vos = wrmNIService.lightCheck(lights);
			logger.info(" running on 线程名：" + Thread.currentThread().getName() + "优先级" + Thread.currentThread().getPriority());
			lightnos.clear();
			break;
		case lightSend://执行安灯线程并打印信息
			lights = JSONArray.toJSONString(lightvos);
			vos = wrmNIService.lightSend(lights);
			logger.info(" running on 线程名：" + Thread.currentThread().getName() + "优先级" + Thread.currentThread().getPriority());
			lightvos.clear();
			break;
		case lightClear://执行清灯线程并打印信息
			lights = String.join(",", lightnos);
			vos = wrmNIService.lightClear(String.join(",", lightnos));
			logger.info(" running on 线程名：" + Thread.currentThread().getName() + "优先级" + Thread.currentThread().getPriority());
			lightnos.clear();
			break;
		case lightSearch:////执行寻灯线程并打印信息
			lights = String.join(",", lightnos);
			vos = wrmNIService.lightSearch(String.join(",", lightnos));
			logger.info(" running on 线程名：" + Thread.currentThread().getName() + "优先级" + Thread.currentThread().getPriority());
			lightnos.clear();
			break;
			default://否则属于无效操作命令
			logger.info(" running on 操作类型不合法，无效线程：" + Thread.currentThread().getName());
			break;
		}
//		logger.info(JSONArray.toJSONString(vos));
		return vos;
	}
	
	public JSONArray getLightvos() {
		return lightvos;
	}
	public void setLightvos(JSONArray lightvos) {
		this.lightvos = lightvos;
	}
	 
	
}
